package app.modules.security.application.web;

import app.modules.common.dto.web.WebIdDTO;
import app.modules.common.util.web.PageUtil;
import app.modules.security.business.service.permission.PermissionResourceRelationService;
import app.modules.security.business.service.permission.PermissionService;
import app.modules.security.dto.web.permission.WebAssignResourceDTO;
import app.modules.security.dto.web.permission.WebPermissionDTO;
import app.modules.security.dto.web.permission.WebPermissionQueryDTO;
import app.modules.security.dto.web.permission.WebPermissionSaveDTO;
import app.modules.security.dto.web.permission.WebPermissionUpdateDTO;
import app.modules.security.dto.web.resource.WebResourceDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jasmine.framework.web.entity.WebResult;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;


/**
 * @author mh.z
 */
@Tag(name = "接口管理/权限API")
@RestController
public class PermissionController {
    private final PermissionService permissionService;
    private final PermissionResourceRelationService permissionResourceRelationService;

    public PermissionController(PermissionService permissionService,
                                PermissionResourceRelationService permissionResourceRelationService) {
        this.permissionService = permissionService;
        this.permissionResourceRelationService = permissionResourceRelationService;
    }

    @Operation(summary = "分页查询权限")
    @PostMapping("/api/security/permission/pagePermissionsByCond")
    public ResponseEntity<WebResult<List<WebPermissionDTO>>> pageWebPermissionDTOsByCond(
            @RequestBody WebPermissionQueryDTO queryDTO) {
        Page page = PageUtil.getPage(queryDTO);

        List<WebPermissionDTO> recordList = permissionService.pageWebPermissionDTOsByCond(queryDTO, page);
        WebResult result = PageUtil.getResult(recordList, page);

        return result.toOkEntity();
    }

    @Operation(summary = "查询指定 ID 的权限")
    @PostMapping("/api/security/permission/getPermissionById")
    public ResponseEntity<WebResult<WebPermissionDTO>> getPermissionById(
            @RequestBody WebIdDTO dto) {
        Long id = dto.getId();
        WebPermissionDTO permissionDTO = permissionService.getWebPermissionDtoById(id);

        return WebResult.success(permissionDTO).toOkEntity();
    }

    @Operation(summary = "保存权限")
    @PostMapping("/api/security/permission/savePermission")
    public ResponseEntity<WebResult<WebPermissionDTO>> savePermission(
            @Valid @RequestBody WebPermissionSaveDTO saveDTO) {
        WebPermissionDTO permissionDTO = permissionService.savePermission(saveDTO);
        WebResult result = WebResult.success(permissionDTO);

        return result.toOkEntity();
    }

    @Operation(summary = "更新权限")
    @PostMapping("/api/security/permission/updatePermission")
    public ResponseEntity<WebResult<WebPermissionDTO>> updatePermission(
            @Valid @RequestBody WebPermissionUpdateDTO updateDTO) {
        WebPermissionDTO permissionDTO = permissionService.updatePermission(updateDTO);
        WebResult result = WebResult.success(permissionDTO);

        return result.toOkEntity();
    }

    @Operation(summary = "删除指定 ID 的权限")
    @PostMapping("/api/security/permission/deletePermissionById")
    public ResponseEntity<WebResult<Object>> deletePermissionById(
            @RequestBody WebIdDTO paramDTO) {
        Long recordId = paramDTO.getId();
        permissionService.deleteRecordById(recordId);

        return WebResult.success().toOkEntity();
    }

    @Operation(summary = "查询指定权限的所有已分配资源")
    @PostMapping("/api/security/permission/listAssignedResources")
    public ResponseEntity<WebResult<List<WebResourceDTO>>> listAssignedResources(
            @RequestBody WebIdDTO paramDTO) {
        Long recordId = paramDTO.getId();
        List<WebResourceDTO> recordList = permissionResourceRelationService.listAssignedResources(recordId);

        return WebResult.success(recordList).toOkEntity();
    }

    @Operation(summary = "向指定权限分配资源")
    @PostMapping("/api/security/permission/assignResources")
    public ResponseEntity<WebResult<Object>> assignResources(
            @RequestBody WebAssignResourceDTO assignDTO) {
        Long permissionId = assignDTO.getPermissionId();
        List<Long> resourceIdList = assignDTO.getResourceIds();
        permissionResourceRelationService.assignResource(permissionId, resourceIdList);

        return WebResult.success().toOkEntity();
    }

}
